<?php

namespace app\common\logic;

use think\Model;

class KqBuqian extends Model
{
    protected $autoWriteTimestamp = true;
    protected $insert = ['uid'];

    protected function setUidAttr($value)
    {
        return getUid();
    }

    protected function setSpTimeAttr($value)
    {
        if (empty($value)) return 0;
        return strtotime($value);
    }

    protected function getSpTimeAttr($value)
    {
        if (empty($value)) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    protected function setTimeAttr($value)
    {
        if (empty($value)) return 0;
        return strtotime($value);
    }

    protected function getTimeAttr($value)
    {
        if (empty($value)) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    protected function getCreateTimeAttr($value)
    {
        if (empty($value)) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    protected function getUpdateTimeAttr($value)
    {
        if (empty($value)) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * 新增
     * @param $data
     * @return bool|int
     * @author wb <pithyone@vip.qq.com>
     */
    public function addItem($data)
    {
        $ret = $this->allowField(true)->isUpdate(false)->save($data);
        return $ret;
    }

    /**
     * 根据用户id返回这个月补签次数
     * @param $uid
     * @return int
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getCountInMonth($uid)
    {
        $month_start = strtotime(date('Y-m-01', time()));
        $month_end = strtotime(date('Y-m-30', time()));

        $where['uid'] = $uid;
        $where['create_time'] = ['between', [$month_start, $month_end]];
        $ret = self::where($where)->count();

        return $ret;
    }

    public static function getList($where, $order)
    {
        $rows = empty(input('rows')) ? config('paginate.list_rows') : input('rows');
        $list = self::where($where)->order($order)->paginate($rows);
        $ret = $list->render();
        return $ret;
    }
}